package com.njupt.Array;

import java.util.Arrays;

public class Rotate_189 {

    public void rotate(int[] nums, int k) {


        if(nums.length < k){
            while(k > 0){
                rotateOne(nums);
                k--;
            }
            return;
        }

        int[] temp = Arrays.copyOfRange(nums, nums.length - k, nums.length);

        for(int i = nums.length - k - 1;i >= 0;i--){
            nums[i + k] = nums[i];
        }

        for(int i = 0;i < k;i++){
            nums[i] = temp[i];
        }
    }


    public void rotate1(int[] nums, int k){
        int len = nums.length;
        int[] newArr = new int[nums.length];
        for(int i = 0;i < nums.length;i++){
            newArr[ (i + k) % len] = nums[i];
        }
        System.arraycopy(newArr,0,nums,0,len);
    }

    public void rotateOne(int[] nums){
        int temp = nums[nums.length - 1];
        for(int i = nums.length - 1 ;i > 0;i--){
            if(i > 0){
                nums[i] = nums[i - 1];
            }
        }
        nums[0] = temp;
    }


    public static void main(String[] args) {
        int[] nums = {1,2,3,4,5,6,7};
        Rotate_189 test = new Rotate_189();
        test.rotate1(nums,3);
        for (int i = 0; i < nums.length; i++) {
            System.out.print(nums[i] + " ");
        }
    }
}
